package com.gxl.butt.thread;

import java.util.concurrent.*;

import static com.gxl.butt.value.StaticValue.CACHED_THREAD_POOL;
import static com.gxl.butt.value.StaticValue.SINGLE_THREAD_EXECUTOR;
import static com.gxl.butt.value.StaticValue.THREAD_EXEC;

public class ExecutorThreadPool {

    public static void main(String args[]){

        try {
            System.out.println("固定数量线程 " + fixedThreadPool(new Callable<Boolean>() {
                @Override
                public Boolean call() throws Exception {
                    System.out.println("固定数量线程任务-执行线程" + Thread.currentThread().getName());
                    return true;
                }
            }));
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }


//        try {
//            for (int i = 0 ;i<10;i++){
//                System.out.println("单线程 " + singleThreadPool(new Callable<Boolean>() {
//                    @Override
//                    public Boolean call() throws Exception {
//                        System.out.println("单线程任务-执行线程" + Thread.currentThread().getName());
//                        return true;
//                    }
//                }));
//            }
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        } catch (ExecutionException e) {
//            e.printStackTrace();
//        }



//        try {
//            for (int i = 0 ;i<200;i++) {
//                System.out.println("可缓存线程 " + cachedThreadPool(new Callable<Boolean>() {
//                    @Override
//                    public Boolean call() throws Exception {
//                        System.out.println("可缓存线程任务-执行线程" + Thread.currentThread().getName());
//                        return true;
//                    }
//                }));
//            }
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        } catch (ExecutionException e) {
//            e.printStackTrace();
//        }
    }

    /** 固定数量线程任务 */
    public static boolean fixedThreadPool(Callable<Boolean> callable)throws InterruptedException,ExecutionException {

        //提交任务
        Future<Boolean> task = THREAD_EXEC.submit(callable);
        return task.get();

    }


    /* 单线程任务 */
    public static boolean singleThreadPool(Callable<Boolean> callable)throws InterruptedException,ExecutionException {

        //提交任务
        Future<Boolean> task = SINGLE_THREAD_EXECUTOR.submit(callable);
        return task.get();

    }

    /* 可缓存线程任务 */
    public static boolean cachedThreadPool(Callable<Boolean> callable)throws InterruptedException,ExecutionException {

        //提交任务
        Future<Boolean> task = CACHED_THREAD_POOL.submit(callable);
        return task.get();

    }


    //模拟耗时操作，定义一个斐波那契数列
    private static int fibc(int num){
        if (num == 0){
            return 0;
        }
        if (num == 1){
            return  1;
        }
        return fibc(num-1)+fibc(num-2);
    }
 
}